package org.sxp.common.utils.datax.reader;


import cn.hutool.core.util.StrUtil;
import org.sxp.common.exception.BusinessException;
import org.sxp.modules.datasource.entity.DatabaseDO;
import org.sxp.modules.dataworks.dto.SyncDTO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * mysql reader 构建类
 *
 * @author zhouhongfa@gz-yibo.com
 * @ClassName MysqlReader
 * @Version 1.0
 * @since 2019/7/30 23:07
 */
public class OceanBaseReader extends BaseReader implements IDataxReader {

    public static final String OB10_SPLIT_STRING = "||_dsc_ob10_dsc_||";
    public static final String OB10_SPLIT_STRING_PATTERN = "\\|\\|_dsc_ob10_dsc_\\|\\|";

    public OceanBaseReader(SyncDTO syncDTO) {
        super(syncDTO);
    }

    @Override
    public String getName() {
        return "oceanbasev10reader";
    }

    @Override
    protected void afterBuild(DatabaseDO jobDatasource, Map<String, Object> readerObj) throws BusinessException {
        if((StrUtil.isBlank(jobDatasource.getClusterName()) && StrUtil.isNotBlank(jobDatasource.getTenantName())) ||
                (StrUtil.isNotBlank(jobDatasource.getClusterName()) && StrUtil.isBlank(jobDatasource.getTenantName()))){
            throw new BusinessException("数据源配置错误！集群名和租户名应同时使用，或者均不填。");
        }else if(StrUtil.isNotBlank(jobDatasource.getClusterName())){
            Map<String, Object> parameter = (Map<String, Object>)readerObj.get("parameter");
            List<Map<String, Object>> connectionObj = (List<Map<String, Object>>)parameter.get("connection");
            List<String> jdbcUrlList = (List<String>)connectionObj.get(0).get("jdbcUrl");
            String newUrl = OB10_SPLIT_STRING+jobDatasource.getClusterName()+":"+jobDatasource.getTenantName()+OB10_SPLIT_STRING+jdbcUrlList.get(0);

            List<String> newJdbcUrlList = new ArrayList<>();
            newJdbcUrlList.add(newUrl);
            connectionObj.get(0).put("jdbcUrl", newJdbcUrlList);
        }else{

        }
    }
}
